package com.example.configuration;

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.InvocationHandler;

import java.lang.reflect.Method;

/**
 * @Date 2022/1/24
 * @Created by Jonathan
 */
public class CGlibMain {
	public static void main(String[] args) {
		/*
		 * 1. 原始对象
		 * 2. 基于接口还是基于继承父类
		 * 3. 类加载器
		 * 4. 额外功能
		 */
		Enhancer enhancer = new Enhancer();
		A a = new A();
		enhancer.setSuperclass(A.class);
		enhancer.setClassLoader(CGlibMain.class.getClassLoader());
		enhancer.setCallback(new InvocationHandler() {
			@Override
			@SuppressWarnings("all")
			public Object invoke(Object o, Method method, Object[] objects) throws Throwable {
				System.out.println("增加额外功能");
				return method.invoke(a, objects);
			}
		});
		A o = (A) enhancer.create();
		o.sayHello();
	}
}
